/*********************************************************************************
  *FileName:  nsuc16xx_epwm.c
  *Author:    ChenJiehong
  *Version:   V1.0
  *Date:      2023-09-22
****************************************Includes***********************************/
#include "nsuc16xx_epwm.h"

/***************************************Variables***********************************/

/***************************************Functions***********************************/

/**
  * @brief  Initializes the EPWM peripheral according to the specified parameters
  *         in the EPWM_InitStruct.
  * @param  EPWMx: Select the EPWM peripheral.
  * @param  EPWM_InitStruct: pointer to an EPWM_InitStruct structure that contains
  *         the configuration information for the specified EPWM peripheral.
  * @retval None
  */
void EPWM_Init(EPWM_TypeDef *EPWMx, EPWM_InitTypeDef *EPWM_InitStruct)
{
    EPWMx->CR0_b.FSOFF = EPWM_InitStruct->EPWM_OFF_SlewRate;
    EPWMx->CR0_b.FSON = EPWM_InitStruct->EPWM_ON_SlewRate;
    EPWMx->CR0_b.OCFT = EPWM_InitStruct->EPWM_OC_FilterTime;
    EPWMx->CR0_b.CKSEL = EPWM_InitStruct->EPWM_ClockSource;
    EPWMx->CR0_b.CCPT = EPWM_InitStruct->EPWM_CC_ProtectionTime;
    EPWMx->CR0_b.OCSDM = EPWM_InitStruct->EPWM_OC_ShutDownMode;
    EPWMx->CR0_b.BCM = EPWM_InitStruct->EPWM_BridgeArmMode;
    EPWMx->CR0_b.PAM = EPWM_InitStruct->EPWM_CounterMode;
}

/**
  * @brief  Enables or disables the specified EPWM peripheral.
  * @param  EPWMx: Select the EPWM peripheral.
  * @param  NewState: new state of the EPWM peripheral.
  *   This parameter can be: ENABLE or DISABLE.
  * @retval None
  */
void EPWM_Cmd(EPWM_TypeDef *EPWMx, FunctionalState NewState)
{
    if (NewState != DISABLE)
    {
        EPWMx->CR0_b.EN = 1;
    }
    else
    {
        EPWMx->CR0_b.EN = 0;
    }
}

/**
  * @brief  Initializes the HalfBridge0 according to the specified parameters
  * @param  EPWMx: Select the EPWM peripheral.
  * @param  HalfBridge_InitStruct: pointer to an HalfBridge_InitStruct structure that contains
  *         the configuration information for the specified HalfBridge0 peripheral.    
  * @retval None
  */
void EPWM_HalfBridge0_Init(EPWM_TypeDef *EPWMx, HalfBridge_InitTypeDef *HalfBridge_InitStruct)
{
    if (HalfBridge_InitStruct->HalfBridge_CurrentLimit != DISABLE)
    {
        EPWMx->CR1_b.CLM0 = 1;
    }
    else
    {
        EPWMx->CR1_b.CLM0 = 0;
    }
    EPWMx->CR1_b.CLS0 = HalfBridge_InitStruct->HalfBridge_CurrentLimitSource;

    EPWMx->CR1_b.LSOM0 = HalfBridge_InitStruct->HalfBridge_LowSideOutMode;
    EPWMx->CR1_b.HSOM0 = HalfBridge_InitStruct->HalfBridge_HighSideOutMode;

    EPWMx->PER0 = HalfBridge_InitStruct->HalfBridge_Period;
    EPWMx->CMP0 = HalfBridge_InitStruct->HalfBridge_Compare;
}

/**
  * @brief  Initializes the HalfBridge1 according to the specified parameters
  * @param  EPWMx: Select the EPWM peripheral.
  * @param  HalfBridge_InitStruct: pointer to an HalfBridge_InitStruct structure that contains
  *         the configuration information for the specified HalfBridge1 peripheral.    
  * @retval None
  */
void EPWM_HalfBridge1_Init(EPWM_TypeDef *EPWMx, HalfBridge_InitTypeDef *HalfBridge_InitStruct)
{
    if (HalfBridge_InitStruct->HalfBridge_CurrentLimit != DISABLE)
    {
        EPWMx->CR1_b.CLM1 = 1;
    }
    else
    {
        EPWMx->CR1_b.CLM1 = 0;
    }
    EPWMx->CR1_b.CLS1 = HalfBridge_InitStruct->HalfBridge_CurrentLimitSource;

    EPWMx->CR1_b.LSOM1 = HalfBridge_InitStruct->HalfBridge_LowSideOutMode;
    EPWMx->CR1_b.HSOM1 = HalfBridge_InitStruct->HalfBridge_HighSideOutMode;

    EPWMx->PER1 = HalfBridge_InitStruct->HalfBridge_Period;
    EPWMx->CMP1 = HalfBridge_InitStruct->HalfBridge_Compare;
}

/**
  * @brief  Initializes the HalfBridge2 according to the specified parameters
  * @param  EPWMx: Select the EPWM peripheral.
  * @param  HalfBridge_InitStruct: pointer to an HalfBridge_InitStruct structure that contains
  *         the configuration information for the specified HalfBridge2 peripheral.    
  * @retval None
  */
void EPWM_HalfBridge2_Init(EPWM_TypeDef *EPWMx, HalfBridge_InitTypeDef *HalfBridge_InitStruct)
{
    if (HalfBridge_InitStruct->HalfBridge_CurrentLimit != DISABLE)
    {
        EPWMx->CR1_b.CLM2 = 1;
    }
    else
    {
        EPWMx->CR1_b.CLM2 = 0;
    }
    EPWMx->CR1_b.CLS2 = HalfBridge_InitStruct->HalfBridge_CurrentLimitSource;

    EPWMx->CR1_b.LSOM2 = HalfBridge_InitStruct->HalfBridge_LowSideOutMode;
    EPWMx->CR1_b.HSOM2 = HalfBridge_InitStruct->HalfBridge_HighSideOutMode;

    EPWMx->PER2 = HalfBridge_InitStruct->HalfBridge_Period;
    EPWMx->CMP2 = HalfBridge_InitStruct->HalfBridge_Compare;
}

/**
  * @brief  Initializes the HalfBridge3 according to the specified parameters
  * @param  EPWMx: Select the EPWM peripheral.
  * @param  HalfBridge_InitStruct: pointer to an HalfBridge_InitStruct structure that contains
  *         the configuration information for the specified HalfBridge3 peripheral.    
  * @retval None
  */
void EPWM_HalfBridge3_Init(EPWM_TypeDef *EPWMx, HalfBridge_InitTypeDef *HalfBridge_InitStruct)
{
    if (HalfBridge_InitStruct->HalfBridge_CurrentLimit != DISABLE)
    {
        EPWMx->CR1_b.CLM3 = 1;
    }
    else
    {
        EPWMx->CR1_b.CLM3 = 0;
    }
    EPWMx->CR1_b.CLS3 = HalfBridge_InitStruct->HalfBridge_CurrentLimitSource;

    EPWMx->CR1_b.LSOM3 = HalfBridge_InitStruct->HalfBridge_LowSideOutMode;
    EPWMx->CR1_b.HSOM3 = HalfBridge_InitStruct->HalfBridge_HighSideOutMode;

    EPWMx->PER3 = HalfBridge_InitStruct->HalfBridge_Period;
    EPWMx->CMP3 = HalfBridge_InitStruct->HalfBridge_Compare;
}

/**
  * @brief  Configure the EPMW HalfBridge0 high-side and low-side output modes
  * @param  EPWMx: Select the EPWM peripheral.
  * @param  LSOM: low-side output modes
  * @param  HSOM: high-side output modes
  * @retval None
  */
void EPWM_HalfBridge0Config(EPWM_TypeDef *EPWMx, uint8_t LSOM, uint8_t HSOM)
{
    EPWMx->CR1_b.LSOM0 = LSOM;
    EPWMx->CR1_b.HSOM0 = HSOM;
}

/**
  * @brief  Configure the EPMW HalfBridge1 high-side and low-side output modes
  * @param  EPWMx: Select the EPWM peripheral.
  * @param  LSOM: low-side output modes
  * @param  HSOM: high-side output modes
  * @retval None
  */
void EPWM_HalfBridge1Config(EPWM_TypeDef *EPWMx, uint8_t LSOM, uint8_t HSOM)
{
    EPWMx->CR1_b.LSOM1 = LSOM;
    EPWMx->CR1_b.HSOM1 = HSOM;
}

/**
  * @brief  Configure the EPMW HalfBridge2 high-side and low-side output modes
  * @param  EPWMx: Select the EPWM peripheral.
  * @param  LSOM: low-side output modes
  * @param  HSOM: high-side output modes
  * @retval None
  */
void EPWM_HalfBridge2Config(EPWM_TypeDef *EPWMx, uint8_t LSOM, uint8_t HSOM)
{
    EPWMx->CR1_b.LSOM2 = LSOM;
    EPWMx->CR1_b.HSOM2 = HSOM;
}

/**
  * @brief  Configure the EPMW HalfBridge3 high-side and low-side output modes
  * @param  EPWMx: Select the EPWM peripheral.
  * @param  LSOM: low-side output modes
  * @param  HSOM: high-side output modes
  * @retval None
  */
void EPWM_HalfBridge3Config(EPWM_TypeDef *EPWMx, uint8_t LSOM, uint8_t HSOM)
{
    EPWMx->CR1_b.LSOM3 = LSOM;
    EPWMx->CR1_b.HSOM3 = HSOM;
}

/**
  * @brief  Sets the EPWM Compare Setting Register 0 value
  * @param  EPWMx: Select the EPWM peripheral.
  * @param  LSOM: low-side output modes
  * @param  HSOM: high-side output modes
  * @retval None
  */
void EPWM_SetCompare0(EPWM_TypeDef *EPWMx, uint16_t EPWM_Compare)
{
    EPWMx->CMP0 = EPWM_Compare;
}

/**
  * @brief  Sets the EPWM Compare Setting Register 1 value
  * @param  EPWMx: Select the EPWM peripheral.
  * @param  LSOM: low-side output modes
  * @param  HSOM: high-side output modes
  * @retval None
  */
void EPWM_SetCompare1(EPWM_TypeDef *EPWMx, uint16_t EPWM_Compare)
{
    EPWMx->CMP1 = EPWM_Compare;
}

/**
  * @brief  Sets the EPWM Compare Setting Register 2 value
  * @param  EPWMx: Select the EPWM peripheral.
  * @param  LSOM: low-side output modes
  * @param  HSOM: high-side output modes
  * @retval None
  */
void EPWM_SetCompare2(EPWM_TypeDef *EPWMx, uint16_t EPWM_Compare)
{
    EPWMx->CMP2 = EPWM_Compare;
}

/**
  * @brief  Sets the EPWM Compare Setting Register 3 value
  * @param  EPWMx: Select the EPWM peripheral.
  * @param  LSOM: low-side output modes
  * @param  HSOM: high-side output modes
  * @retval None
  */
void EPWM_SetCompare3(EPWM_TypeDef *EPWMx, uint16_t EPWM_Compare)
{
    EPWMx->CMP3 = EPWM_Compare;
}

/**
  * @brief  Configure the EPMW HalfBridge0 trigger mode
  * @param  EPWMx: Select the EPWM peripheral.
  * @param  CounterUp_TrigState: Trigger on counter up count
  * @param  CounterUp_TrigState: Trigger on counter down count
  * @param  EPWM_TrigFilter: Trigger filter value
  * @retval None
  */
void EPWM_Trig0ModeConfig(EPWM_TypeDef *EPWMx, uint8_t CounterUp_TrigState, uint8_t CounterDown_TrigState,
                          uint8_t EPWM_TrigFilter)
{
    EPWMx->TCR_b.DEC0 = EPWM_TrigFilter;
    EPWMx->TCR_b.UCE0 = CounterUp_TrigState;
    EPWMx->TCR_b.DCE0 = CounterDown_TrigState;
}

/**
  * @brief  Configure the EPMW HalfBridge1 trigger mode
  * @param  EPWMx: Select the EPWM peripheral.
  * @param  CounterUp_TrigState: Trigger on counter up count
  * @param  CounterUp_TrigState: Trigger on counter down count
  * @param  EPWM_TrigFilter: Trigger filter value
  * @retval None
  */
void EPWM_Trig1ModeConfig(EPWM_TypeDef *EPWMx, uint8_t CounterUp_TrigState, uint8_t CounterDown_TrigState,
                          uint8_t EPWM_TrigFilter)
{
    EPWMx->TCR_b.DEC1 = EPWM_TrigFilter;
    EPWMx->TCR_b.UCE1 = CounterUp_TrigState;
    EPWMx->TCR_b.DCE1 = CounterDown_TrigState;
}

/**
  * @brief  Configure the EPMW HalfBridge2 trigger mode
  * @param  EPWMx: Select the EPWM peripheral.
  * @param  CounterUp_TrigState: Trigger on counter up count
  * @param  CounterUp_TrigState: Trigger on counter down count
  * @param  EPWM_TrigFilter: Trigger filter value
  * @retval None
  */
void EPWM_Trig2ModeConfig(EPWM_TypeDef *EPWMx, uint8_t CounterUp_TrigState, uint8_t CounterDown_TrigState,
                          uint8_t EPWM_TrigFilter)
{
    EPWMx->TCR_b.DEC2 = EPWM_TrigFilter;
    EPWMx->TCR_b.UCE2 = CounterUp_TrigState;
    EPWMx->TCR_b.DCE2 = CounterDown_TrigState;
}

/**
  * @brief  Configure the EPMW HalfBridge3 trigger mode
  * @param  EPWMx: Select the EPWM peripheral.
  * @param  CounterUp_TrigState: Trigger on counter up count
  * @param  CounterUp_TrigState: Trigger on counter down count
  * @param  EPWM_TrigFilter: Trigger filter value
  * @retval None
  */
void EPWM_Trig3ModeConfig(EPWM_TypeDef *EPWMx, uint8_t CounterUp_TrigState, uint8_t CounterDown_TrigState,
                          uint8_t EPWM_TrigFilter)
{
    EPWMx->TCR_b.DEC3 = EPWM_TrigFilter;
    EPWMx->TCR_b.UCE3 = CounterUp_TrigState;
    EPWMx->TCR_b.DCE3 = CounterDown_TrigState;
}

/**
  * @brief  Configure the EPMW HalfBridge0 trigger value
  * @param  EPWMx: Select the EPWM peripheral.
  * @param  EPWM_TrigTime: Trigger value
  * @retval None
  */
void EPWM_Trig0TimeConfig(EPWM_TypeDef *EPWMx, uint16_t EPWM_TrigTime)
{
    EPWMx->TRG0 = EPWM_TrigTime;
}

/**
  * @brief  Configure the EPMW HalfBridge1 trigger value
  * @param  EPWMx: Select the EPWM peripheral.
  * @param  EPWM_TrigTime: Trigger value
  * @retval None
  */
void EPWM_Trig1TimeConfig(EPWM_TypeDef *EPWMx, uint16_t EPWM_TrigTime)
{
    EPWMx->TRG1 = EPWM_TrigTime;
}

/**
  * @brief  Configure the EPMW HalfBridge2 trigger value
  * @param  EPWMx: Select the EPWM peripheral.
  * @param  EPWM_TrigTime: Trigger value
  * @retval None
  */
void EPWM_Trig2TimeConfig(EPWM_TypeDef *EPWMx, uint16_t EPWM_TrigTime)
{
    EPWMx->TRG2 = EPWM_TrigTime;
}

/**
  * @brief  Configure the EPMW HalfBridge3 trigger value
  * @param  EPWMx: Select the EPWM peripheral.
  * @param  EPWM_TrigTime: Trigger value
  * @retval None
  */
void EPWM_Trig3TimeConfig(EPWM_TypeDef *EPWMx, uint16_t EPWM_TrigTime)
{
    EPWMx->TRG3 = EPWM_TrigTime;
}

void EPWM_MinimumOnTime0Cmd(EPWM_TypeDef *EPWMx, FunctionalState NewState)
{
    if (NewState != DISABLE)
    {
        EPWMx->MOT_b.MEN0 = 1;
    }
    else
    {
        EPWMx->MOT_b.MEN0 = 0;
    }
}



void EPWM_MinimumOnTime1Cmd(EPWM_TypeDef *EPWMx, FunctionalState NewState)
{
    if (NewState != DISABLE)
    {
        EPWMx->MOT_b.MEN1 = 1;
    }
    else
    {
        EPWMx->MOT_b.MEN1 = 0;
    }
}

void EPWM_MinimumOnTime2Cmd(EPWM_TypeDef *EPWMx, FunctionalState NewState)
{
    if (NewState != DISABLE)
    {
        EPWMx->MOT_b.MEN2 = 1;
    }
    else
    {
        EPWMx->MOT_b.MEN2 = 0;
    }
}

void EPWM_MinimumOnTime3Cmd(EPWM_TypeDef *EPWMx, FunctionalState NewState)
{
    if (NewState != DISABLE)
    {
        EPWMx->MOT_b.MEN3 = 1;
    }
    else
    {
        EPWMx->MOT_b.MEN3 = 0;
    }
}

void EPWM_MinimumOnTimeConfig(EPWM_TypeDef *EPWMx, uint16_t MinimumOnTime)
{
    EPWMx->MOT = MinimumOnTime;
}

FlagStatus EPWM_Get0C0tatus(EPWM_TypeDef *EPWMx)
{
    FlagStatus bitstatus = RESET;
    if (EPWMx->OC_b.OC0 == 1)
    {
        bitstatus = SET;
    }
    else
    {
        bitstatus = RESET;
    }
    return bitstatus;
}

FlagStatus EPWM_Get1C0tatus(EPWM_TypeDef *EPWMx)
{
    FlagStatus bitstatus = RESET;
    if (EPWMx->OC_b.OC1 == 1)
    {
        bitstatus = SET;
    }
    else
    {
        bitstatus = RESET;
    }
    return bitstatus;
}

FlagStatus EPWM_GetOC2tatus(EPWM_TypeDef *EPWMx)
{
    FlagStatus bitstatus = RESET;
    if (EPWMx->OC_b.OC2 == 1)
    {
        bitstatus = SET;
    }
    else
    {
        bitstatus = RESET;
    }
    return bitstatus;
}

FlagStatus EPWM_GetOC3tatus(EPWM_TypeDef *EPWMx)
{
    FlagStatus bitstatus = RESET;
    if (EPWMx->OC_b.OC3 == 1)
    {
        bitstatus = SET;
    }
    else
    {
        bitstatus = RESET;
    }
    return bitstatus;
}


void EPWM_IT0Config(EPWM_TypeDef *EPWMx, uint32_t EPWM_IT, FunctionalState NewState)
{
    if (NewState != DISABLE)
    {
        EPWMx->IEN0 |= EPWM_IT;
    }
    else
    {
        EPWMx->IEN0 &= (~(uint32_t)EPWM_IT);
    } 
}

void EPWM_IT1Config(EPWM_TypeDef *EPWMx, uint32_t EPWM_IT, FunctionalState NewState)
{
    if (NewState != DISABLE)
    {
        EPWMx->IEN1 |= EPWM_IT;
    }
    else
    {
        EPWMx->IEN1 &= (~(uint32_t)EPWM_IT);
    } 
}

void EPWM_IT2Config(EPWM_TypeDef *EPWMx, uint32_t EPWM_IT, FunctionalState NewState)
{
    if (NewState != DISABLE)
    {
        EPWMx->IEN2 |= EPWM_IT;
    }
    else
    {
        EPWMx->IEN2 &= (~(uint32_t)EPWM_IT);
    } 
}

void EPWM_IT3Config(EPWM_TypeDef *EPWMx, uint32_t EPWM_IT, FunctionalState NewState)
{
    if (NewState != DISABLE)
    {
        EPWMx->IEN3 |= EPWM_IT;
    }
    else
    {
        EPWMx->IEN3 &= (~(uint32_t)EPWM_IT);
    } 
}

/***************************************END OF FILE*********************************/
